package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.impl.ExamExamDetailServiceImpl;
import com.example.demo.service.impl.ExamExamServiceImpl;
import com.example.demo.service.impl.ExamGroupServiceImpl;
import com.example.demo.service.impl.ExamUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/detail")
@RequiredArgsConstructor
@Component
public class ExamExamDetailController {
    private final ExamExamDetailServiceImpl examExamDetailService;
    private final ExamExamServiceImpl examExamService;
    private final ExamUserServiceImpl examUserService;
    private final ExamGroupServiceImpl examGroupService;
    @PostMapping("/savedetail")
    public void saveDetail(@RequestBody ExamExamDetail examExamDetail) {
        examExamDetail.setIssubmit(1);
        examExamDetailService.save(examExamDetail);
    }

    @GetMapping("/selectdetail")
    public List<ExamDetailList>  selectByexamId(Integer id) {
        List<ExamUserDetail> list=examExamDetailService.selectByexamId(id);
        List<ExamDetailList> list1=new ArrayList<>();
        if(list.size()!=0){
            for (int i = 0; i < list.size(); i++) {
                ExamDetailList examDetailList=new ExamDetailList();
                examDetailList.setId(list.get(i).getId());
                examDetailList.setExamid((list.get(i).getExamid()));
                examDetailList.setIssubmit(list.get(i).getIssubmit());
                examDetailList.setUserid(list.get(i).getUserid());
                examDetailList.setQuestioninfo(list.get(i).getQuestioninfo());
                ExamUser examUser=new ExamUser();
                examUser.setId(list.get(i).getUserid());
                examUser.setIdcard(list.get(i).getIdcard());
                examUser.setPasswords(list.get(i).getPasswords());
                examUser.setGroupid(list.get(i).getGroupid());
                examUser.setUsername(list.get(i).getUsername());
                examUser.setUsersex(list.get(i).getUsersex());
                examUser.setCompanyname(list.get(i).getCompanyname());
                examUser.setPhonenumber(list.get(i).getPhonenumber());
                examDetailList.setUserinfo(examUser);
                list1.add(examDetailList);
            }
            return  list1;
        }

        return new ArrayList<>();
    }

    @GetMapping("/selectgrades")
    public List<ExamUserDetail>  selectGrades(Integer id) {
        ExamExam examExam=examExamService.selectById(id);
        List<ExamUser> list=examUserService.selectGroup(examExam.getGroupid());
        List<ExamUserDetail> list1=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            ExamExamDetail examExamDetail=examExamDetailService.selectTakeExam(list.get(i).getIdcard(),id);
            ExamUserDetail examUserDetail=new ExamUserDetail();

            examUserDetail.setIdcard(list.get(i).getIdcard());
            examUserDetail.setUsername(list.get(i).getUsername());
            examUserDetail.setCompanyname(list.get(i).getCompanyname());
            examUserDetail.setUsersex(list.get(i).getUsersex());
            examUserDetail.setPhonenumber(list.get(i).getPhonenumber());
            if(examExamDetail!=null){
                examUserDetail.setQuestioninfo(examExamDetail.getQuestioninfo());
                examUserDetail.setId(examExamDetail.getId());
            }
            list1.add(examUserDetail);
        }

        return list1;
    }
}
